package me.zixing.java.chapter04.task02.dao;

import me.zixing.java.chapter04.task02.entity.Student;

/**
 * Created by bingh on 2017/10/12.
 */
public class StudentDaoWithArrayStoreImpl implements StudentDao {

    private Student[] students = new Student[10];
    private int point = 0;

    @Override
    public void addStudent(Student student) {

        if(!isFully()){
            if(!hasStudentAlready(student.getStudentId())){
                students[point++] = student;
                System.out.println("添加成功");
            }else{
                System.out.println("该学号学生已经存在");
            }
        }else{
            System.out.println("存储已满,不能添加");
        }

    }

    @Override
    public Student getStudentById(int id) {
        Student student = null;
        for (int i=0;i<point;i++){
            if(students[i].getStudentId() == id){
                student = students[i];
            }
        }
        return student;
    }

    @Override
    public boolean isFully(){
        return point < students.length ? false : true;
    }

    @Override
    public boolean hasStudentAlready(int id){
        for(int i=0;i<point;i++){
            if(students[i].getStudentId() == id){
                return true;
            }
        }
        return false;
    }

}
